#!/usr/bin/env python

#
# (c) 2008 Simon Peter <probono@myrealbox.com>
# GPL v2
#

import sys, os, string
from subprocess import *
# from xml.etree import ElementTree # cannot handle multiple namespaces as ymp has
from xml.dom.minidom import *

try:
        import pygtk
        pygtk.require("2.0")
except:
        pass
try:
        import gtk
        import gtk.glade
except:
        sys.exit(1)

#
# Define the about dialog
#

def showAbout(self):
                dlg = gtk.AboutDialog()
                dlg.set_version("0.0.1") 
                dlg.set_name("ympclient")
                icon = gtk.gdk.pixbuf_new_from_file("get_pack.png") 
                dlg.set_logo(icon)
                dlg.set_authors(["Simon Peter <probono@myrealbox.com>"])

                dlg.set_comments('''Cross-distribution ymp client\nfor the openSUSE 1-Click Install''')
                def close(w, res):
                        if res == gtk.RESPONSE_CANCEL:
                                w.destroy()
                dlg.connect("response", close)
                dlg.show()


#
# Check if we are root. If we are not, do some magic # fixme
#

#if os.getuid() != 0:             
#        try:
#        	xdg_su = "xdg-su" # fixme
#       		
#       		command =  ["sh", xdg_su, "-c"] + [os.path.join(sys.path[0], sys.argv[0])] + sys.argv[1:]
#       		p = Popen(command)
#       		p.wait()
#		sys.exit(p.returncode)
#        except:
#		print "Cannot become root. Exiting"
#		exit(1)

#
# Parse the ymp file
#

try:
        f = open(sys.argv[1], "r")
        xml = f.read()
        f.close()
        document = parseString(xml)
except:
        print "Cannot parse ymp file"
        exit()   

package = document.getElementsByTagName("software")[0].getElementsByTagName("name")[0].firstChild.nodeValue
# print package

description = document.getElementsByTagName("software")[0].getElementsByTagName("description")[0].firstChild.nodeValue
# print description

repo = document.getElementsByTagName("repositories")[0].getElementsByTagName("url")[0].firstChild.nodeValue
# print repo

#
# Show the main window
#

class MainWindow:
        """Display the MainWindow"""

        def __init__(self):
                
                # Show the MainWindow from the glade file
                self.gladefile = os.path.join(sys.path[0],"ympclient.glade")  
                self.wTree = gtk.glade.XML(self.gladefile)                 
                self.window = self.wTree.get_widget("MainWindow")
                self.window.show()

                # Make the X button work
                self.window.connect("destroy", exit)

		# Make the other buttons work
		dic = { "on_OK_clicked" : gtk.main_quit, # continue with Python script
			"on_cancel_clicked" : exit, # exit Python script
			"on_about_clicked" : showAbout, # show about dialog
			"on_MainWindow_destroy" : gtk.main_quit }
		self.wTree.signal_autoconnect(dic)

                # Fill in the info
		strippeddescription = replace = string.replace(description, '\n', ' ')
                self.wTree.get_widget("TopLabel").set_text(package+": "+strippeddescription)
		self.wTree.get_widget("ExpanderLabel").set_text("Repository to be added: "+repo+"\n"+"Package to be installed: "+package+"\n")

if __name__ == "__main__":
        mymain=MainWindow()
        gtk.main()

#
# Write sources.list and call apt-get
#
        
sourceline = "deb "+repo+" ./" 
# print sourceline

# Write the install script
myfile = file('/tmp/ymp.sh', 'w')
myfile.write("echo '"+sourceline+"'> /etc/apt/sources.list.d/ymp.list"+"\n")
myfile.write("apt-get update"+"\n")
myfile.write("apt-get install "+package+"\n")
myfile.write("rm -rf /etc/apt/sources.list.d/ymp.list"+"\n")
#myfile.write("rm -rf /tmp/ymp.sh"+"\n"	)
myfile.write("sleep 5"+"\n")
#myfile.write("read action"+"\n")
myfile.close()

# Make install script executable and run it
os.system("chmod u+x /tmp/ymp.sh")
os.system("xterm -e sudo /tmp/ymp.sh")


